import { getProductsUrl, payUrl, confirmOrderUrl, getProductDetailsUrl, getSalesBannersUrl, getProductRecommendUrl, getProductCategoriesUrl, pointsRedeemUrl } from "../apis/index"
import { request } from "./http-base";

export enum ProductMediaType {
  IMAGE = 'IMAGE',
  VIDEO = 'VIDEO'
}

export interface ProductMedia {
  isMain: boolean,
  type: ProductMediaType.IMAGE
  url: string
}

export interface ProductDetails {
  createDateTime: string,
  description: string,
  id: number,
  lastUpdateTime: string,
  name: string,
  media: ProductMedia[],
  specAttrs: {
    keyId: number | string,
    keyName: string,
    values: [{
      valueId: number,
      valueName: string
    }]
  },
  specs: [
    {
      id: number,
      price:
      {
        points: number,
        money: number
      },
      specItem:
      {
        keyId: string | number,
        keyValue: string,
        valueId: string | number,
        valueName: string
      },
      stock: number
    }
  ]
}

export interface ProductsRes {
  records: ProductDetails[],
  total: number,
  totalPage: number
}

export enum ProductType {
  PHYSICAL = "PHYSICAL",
  SERVICE = "SERVICE",
}

export interface ProductListGetParams {
  categories?: number,
  is_points_mall?: boolean,
  page: number,
  size: number,
  type?: ProductType,
}

export interface ProductCategories {
  id: number | string,
  name: string
}
export interface Banner {
  url: string,
  productId: string | number
}

export interface OrderParams {
  consignee: {
    address: string,
    name: string,
    phone: string,
    postalCode: string
  },
  products: [
    {
      count: number,
      id: number,
      price: {
        money: number,
        points: number
      },
      specId: number
    }
  ],
  remark: string,
  totalPrice: {
    money: number,
    points: number
  }
}


// 首页推荐获取

export const getProductRecommend = async (params: ProductListGetParams): Promise<ProductsRes> => {
  const res = await request(getProductRecommendUrl, params, "GET");
  return res?.data?.data;
}
// 商城产品列表查询
export const getProducts = async (params: ProductListGetParams): Promise<ProductsRes> => {
  const res = await request(`${getProductsUrl}`, params, "GET");
  return res?.data?.data;
}
// 商城产品详情查询
export const getProductDetails = async (productId: string | number): Promise<ProductDetails[]> => {
  const res = await request(`${getProductDetailsUrl(productId)}`, "GET");
  return res?.data?.data;
}
// 商城产品分类查询
export const getProductCategories = async (): Promise<ProductCategories[]> => {
  const res = await request(`${getProductCategoriesUrl}`, "GET");
  return res?.data?.data;
}
// 商城首页banner

export const getSalesBanners = async (): Promise<Banner[]> => {
  const res = await request(getSalesBannersUrl, "GET");
  return res?.data?.data?.banner;
}

export const pointsRedeem = async (data: OrderParams) => {
  return await request(pointsRedeemUrl, data, 'POST')
}
export const confirmOrder = async (data: OrderParams) => {
  return await request(confirmOrderUrl, data, 'POST')
}
export const pay = async (orderId: number) => {
  const res = await request(payUrl(orderId), undefined, "POST");
  console.log('prepay data', res.data.data)
  res.data.data.success = success => { console.log('success', success) }
  res.data.data.fail = fail => { console.log('fail', fail) }
  res.data.data.complete = complete => { console.log('complete', complete) }
  return res.data.data
}

export const getMainImageUrl = (service: ProductDetails) => {
  const { url = '' } =
    service.media.find(
      (m) => m.type == ProductMediaType.IMAGE
    ) || { url: 'https://manbingguanli.oss-cn-beijing.aliyuncs.com/wenduo/e3b9016d83e34beeabc41cc889cde9b5Group 131.jpg' };
  return url;
}

export const getVideoUrl = (service) => {
  const { url = '' } =
    service.media.find(
      (m) => m.type == ProductMediaType.VIDEO
    ) || {};
  return url;
}
export const getDefaultPriceAndPoint = (product: ProductDetails) => {
  const firstSpec = product.specs.find(s => s) || { price: { money: 0, points: 0 } }
  const { money, points } = firstSpec.price
  return { money, points }
}

export const moneyFormat = (money: number = 0) => {
  return (money / 100).toFixed(2);
}